package com.bonc.apicreator.utils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.bonc.apicreator.entity.RestApi;
import com.bonc.apicreator.entity.RestApiRequest;
import com.bonc.apicreator.entity.RestApiResponse;


/**
 * api文档导出word工具
 *
 * @author gaopengyu
 * @version 2016年9月29日
 * @see DocExportUtil
 * @since
 */
public class DocExportUtil {
    /**
     * 日志对象
     */
    private static Logger LOGGER = LogManager.getLogger(DocExportUtil.class);

    /**
     * 默认模块名称
     */
    private static final String DEF_MODULE_NAME = "default";

    /**
     * @name 组装接口模板数据
     * @Description
     * @CreateDate 2015年11月14日下午2:12:13
     */
    @SuppressWarnings("rawtypes")
    public static Map<String, List<Map>> buildInterTmplData(List<RestApi> interList,
                                                            Map<Long, List<RestApiRequest>> paramInfoMap,
                                                            Map<Long, List<RestApiResponse>> respInfoMap) {
        Map<String, List<Map>> interTmplData = initTmplData();

        List<RestApiRequest> interParamList = null;
        List<RestApiResponse> interRespList = null;
        Long interId = null;
        Map<String, Object> tmplData = null;
        for (RestApi restApi : interList) {
            interId = restApi.getId();
            interParamList = paramInfoMap.get(interId);
            interRespList = respInfoMap.get(interId);
            tmplData = buildInterTmplData1(restApi, interParamList, interRespList);
            interTmplData.get(DEF_MODULE_NAME).add(tmplData);
        }

        if (interTmplData.get(DEF_MODULE_NAME).isEmpty()) {
            interTmplData.remove(DEF_MODULE_NAME);
        }

        return interTmplData;
    }

    /**
     * Description: 初始化模块列表 key:模块名称 value:该模块下的接口信息列表
     * 
     * @return Map<String,List<Map>>
     * @see
     */
    @SuppressWarnings("rawtypes")
    public static Map<String, List<Map>> initTmplData() {
        Map<String, List<Map>> result = new LinkedHashMap<String, List<Map>>();
        // 默认模块，当接口未指定模块时，都归属于当前模块下
        result.put(DEF_MODULE_NAME, new ArrayList<Map>());

        return result;
    }

    /**
     * Description: 组装接口模板数据
     * 
     * @param api
     * @param paramInfoMap
     * @param respInfoMap
     * @return Map<String,Object>
     * @see
     */
    private static Map<String, Object> buildInterTmplData1(RestApi api,
                                                           List<RestApiRequest> paramInfoMap,
                                                           List<RestApiResponse> respInfoMap) {
        Map<String, Object> result = new HashMap<>();
        result.put("path", api.getRequestUrl());
        result.put("scheme", api.getRequestFormat());
        result.put("method", api.getRequestType());
        result.put("summary", "summary");
        result.put("description", api.getDescription());
        result.put("name", api.getName());

        result.put("paramList", buildInterParamTmplData(paramInfoMap));
        result.put("respList", buildInterRespTmplData(respInfoMap));

        return result;
    }

    /**
     * Description: 组装方法参数列表
     * 
     * @param interParamList
     * @return List<Map>
     * @see
     */
    @SuppressWarnings("rawtypes")
    public static List<Map> buildInterParamTmplData(List<RestApiRequest> interParamList) {
        List<Map> paramList = new ArrayList<Map>();
        if (CollectionUtils.isEmpty(interParamList)) {
            return paramList;
        }

        Map<String, Object> paramInfo = null;
        for (RestApiRequest restApiRequest : interParamList) {
            paramInfo = new HashMap<>();
            paramInfo.put("code", restApiRequest.getCode());
            paramInfo.put("description", restApiRequest.getDescription());
            paramInfo.put("position", restApiRequest.getLocation());
            paramInfo.put("type", restApiRequest.getType());
            paramInfo.put("isMust", restApiRequest.getIsMust());

            paramList.add(paramInfo);
        }

        return paramList;
    }

    /**
     * Description: 组装方法响应列表
     * 
     * @param interRespList
     * @return List<Map>
     * @see
     */
    @SuppressWarnings("rawtypes")
    public static List<Map> buildInterRespTmplData(List<RestApiResponse> interRespList) {
        List<Map> respList = new ArrayList<Map>();
        if (CollectionUtils.isEmpty(interRespList)) {
            return respList;
        }

        Map<String, Object> respInfo = null;
        for (RestApiResponse restApiResponse : interRespList) {

            respInfo = new HashMap<>();
            respInfo.put("code", restApiResponse.getCode());
            respInfo.put("type", restApiResponse.getType());
            respInfo.put("description", restApiResponse.getDescription());
            respList.add(respInfo);
        }
        LOGGER.info("响应信息为" + JSON.toJSONString(respList));
        return respList;
    }

}
